package com.mig.coins.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mig.coins.db.Consultas;

public class Pais extends Consultas{
	private long id = 0;
	private String nombre = null;
	private String siglas = null;
	
	public static int SPAIN = 1;
	public static int USA = 2;
	
	private final static String GET_PAIS_SQL = "SELECT `paises`.`id`, `paises`.`pais`, `paises`.`siglas`" +
	"FROM `paises` ";

	private final static String GET_PAIS_DIVISA_SQL = "SELECT DISTINCT `paises`.`id`, `paises`.`pais`, `paises`.`siglas`" +
	"FROM `paises`, `pais_divisa` WHERE (`paises`.`id` = `pais_divisa`.`pais`) ";

	public Long insert(Connection conn) {
		// Validaciones para insertar
		// Insertar
		// Obtiene el id y lo guarda
		return null;
	}

	public boolean delete(Connection conn) {

		return false;
	}

	public boolean update(Connection conn) {
		return false;
	}

	public static Pais readPais(Connection conn, Long _id) throws SQLException {
		Pais _pais = new Pais(); 
		PreparedStatement stmt=null;
		ResultSet rs = null;

		stmt=conn.prepareStatement(GET_PAIS_SQL + "WHERE id=?");
		stmt.setLong(1, _id.longValue());
		rs = stmt.executeQuery();
		if (rs.next()) 
			_pais.loadPaisFromResultSet(rs);
		else 
			_pais = null;

		if (rs != null) rs.close();
		if (stmt != null) stmt.close(); 

		return _pais;
	}

	public static ArrayList<Pais> searchPaises(Connection conn, String where, String sort) throws SQLException {
		String query = GET_PAIS_SQL;
		if ((where != null) && (where.trim().length()>0))
			query +="WHERE " + where;

		return search(conn, query, sort);
	}

	public static ArrayList<Pais> searchPaisesByDivisa(Connection conn, String where, String sort) throws SQLException {
		String query = GET_PAIS_DIVISA_SQL;
		if ((where != null) && (where.trim().length()>0)) 
			query += " AND " + where;

		return search(conn, query, sort);
	}

	private static ArrayList<Pais> search(Connection conn, String sql, String sort) throws SQLException {
		String query = sql;

		if ((sort != null) && (sort.trim().length()>0)) {
			query +="ORDER BY " + sort + " ";
		}
		// Creamos el Statement y el ResultSet.
		Statement stm = conn.createStatement();
		ResultSet rs = stm.executeQuery(query);

		// Procesamos el ResultSet.
		ArrayList<Pais> lPaises = null;
		while (rs.next()) {
			Pais _pais = new Pais();
			_pais.loadPaisFromResultSet(rs);
			if (null == lPaises)
				lPaises = new ArrayList<Pais>();
			lPaises.add(_pais);
		}
		rs.close();
		stm.close();
		return lPaises;
	}

	private void loadPaisFromResultSet(ResultSet rs) throws SQLException {
		this.id = rs.getLong(1);
		this.nombre = rs.getString(2);
		this.siglas = rs.getString(3);
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public String getSiglas() {
		return siglas;
	}

	public void setSiglas(String siglas) {
		this.siglas = siglas;
	}

}
